import br.com.nooclix.salon.ui.dto.service.ServiceTO;
import br.com.nooclix.salon.ui.util.Utilitys;
import br.com.nooclix.salon.ui.util.WindowsMessage;

import mx.controls.Alert;
import mx.controls.TextInput;
import mx.events.CloseEvent;

protected var service:ServiceTO;

protected var bySearch:Boolean = false;

public function save():void{
	
	service = new ServiceTO();
	service.name = fieldName.text;
	service.percentage = Utilitys.toDoublePercentage(fieldPercentage.text);
	service.value = Utilitys.toDouble(fieldValue.text);
	if(service.name == null){
		WindowsMessage.alarm("Informe o nome do serviço!");
		return;
	}
	if(service.value == 0.00){
		WindowsMessage.alarm("Informe o valor do serviço!");
		return;
	}
	serviceRemote.persist(service);
	
}

public function serviceUpdate():void{
	
	service.name = fieldName.text;
	service.percentage = Utilitys.toDoublePercentage(fieldPercentage.text);
	service.value = Utilitys.toDouble(fieldValue.text);
	
	serviceRemote.update(service);
	
}


private function findByName():void{
	
	serviceRemote.findByName(fieldSearch.text);
	
}


private function removeService():void{
	if(gridService.selectedItem == null){
		WindowsMessage.alarmWithTitle("Ops, selecione um(a) serviço para poder excluir.","Exclusão");
		return;
	}
	
	var ser:ServiceTO = gridService.selectedItem as ServiceTO; 
	
	WindowsMessage.question("Deseja excluir o(a) serviço "+ ser.name,"Deleção",function (event:CloseEvent):void{
		if(event.detail == Alert.YES){
			serviceRemote.remove(ser);		
		}
	});
	
	
}

private function showService():void{
	
	if(gridService.selectedItem == null){
		WindowsMessage.alarmWithTitle("Ops, selecione um(a) serviço para visualizá-lo.","Visualização");
		return;
	}
	this.currentState = "update";
	clearFields();
	service = gridService.selectedItem as ServiceTO;
	
	fieldName.text = service.name;
	fieldPercentage.text = Utilitys.formatPercentage(service.percentage);
	fieldValue.text = Utilitys.formatCurrencyBR(service.value);
	
}

private function backSearch():void{
	
	findByName();
	service = null;
	this.currentState = "search";
	
}

private function newService():void{
	
	this.currentState = "create";
	clearFields();
	bySearch = true;
	
}

public function clearFields():void{

	fieldName.text = "";
	fieldPercentage.text = "0.00 %";
	fieldValue.text = "R$ 0.00"
	fieldName.setFocus();
	service = null;
}

public function format(tf:TextInput):void{
	
	var temp:Number=Number(tf.text);
	
	tf.text = Utilitys.formatCurrencyBR(temp);
	
}

public function unformat(tf:TextInput):void{
	
	tf.text = Utilitys.unformat(tf.text);
	
}

public function formatPercentage(tf:TextInput):void{
	
	var tempP:Number=Number(tf.text);
	
	tf.text = Utilitys.formatPercentage(tempP);
	
}

public function unformatPercentage(tf:TextInput):void{
	
	tf.text = Utilitys.unformatPercentage(tf.text);
	
}